package com.coco.mq;

import com.coco.service.MyWebSocket;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author GHOSTLaycoo
 * @date 2020/6/5 - 16:13
 */
@Component
public class ItemListener {

    @Autowired
    private MyWebSocket myWebSocket;

    /**
     * 监听队列中的消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "co.item.notice.queue",durable = "true"),
            exchange = @Exchange(name = "co.notice.exchange",type= ExchangeTypes.TOPIC),
            key = {"item.insert"}
    ))
    public void listenInsert(String videoName){
        if(videoName==null){
            return;
        }
        //处理消息,对静态页进行新增或修改
        try {
            if(myWebSocket!=null){
                myWebSocket.sendInfo(videoName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
